#-*- coding: utf-8 -*
#@author 小测试
#@create 2020-03-04 23:43
"""
封装Assert方法

"""
# from config.config import RESULT_LIST
from ApiDemo.util.get_log import MyLog
import json


class Assertions:
    def __init__(self):
        self.log=MyLog()
    def assert_code(self, code, expected_code):
        """
        验证response状态码
        :param code:
        :param expected_code:
        :return:
        """
        try:
            assert code == expected_code
            return True
        except:
            self.log.error("状态码错误, 预期结果: %s, 实际结果: %s " % (expected_code, code))
            raise

    def assert_body(self, body, body_msg, expected_msg):
        """
        验证response body中任意属性的值
        :param body:
        :param body_msg:
        :param expected_msg:
        :return:
        """
        try:
            msg = body[body_msg]
            assert msg == expected_msg
            return True

        except:
            self.log.error("Response body msg != expected_msg, expected_msg is %s, body_msg is %s" % (expected_msg, body_msg))
            raise

    def assert_in_text(self, body, expected_msg):
        """
        验证response body中是否包含预期字符串
        :param body:
        :param expected_msg:
        :return:
        """
        try:
            if type(body)==dict:
                key=[key for key in body.keys()]
                assert expected_msg in key
            else:
                text = json.dumps(body, ensure_ascii=False)
                assert expected_msg in text
            return True

        except:
            self.log.error("Response body Does not contain expected_msg, expected_msg is %s" % expected_msg)
            raise

    def assert_text(self, body, expected_msg):
        """
        验证response body中是否等于预期字符串
        :param body:
        :param expected_msg:
        :return:
        """
        try:
            assert body == expected_msg
            return True

        except:
            self.log.error("Response body != expected_msg, expected_msg is %s, body is %s" % (expected_msg, body))
            raise

    def assert_time(self, time, expected_time):
        """
        验证response body响应时间小于预期最大响应时间,单位：毫秒
        :param body:
        :param expected_time:
        :return:
        """
        try:
            assert time < expected_time
            return True

        except:
            self.log.error("Response time > expected_time, expected_time is %s, time is %s" % (expected_time, time))
            raise

    def run(self,res_json,expect):
        for key,value in expect.items():
            if key=="code":
                try:
                    code=res_json["code"]
                    self.assert_code(code,value)
                except AssertionError as E:
                # expect AssertionError as E:
                    code=res_json["code"]
                    self.assert_code(code,value)
            elif key=="msg":
                msg=res_json["msg"]
                self.assert_text(msg,value)
            elif key=="data":
                data=res_json["data"]
                for data_key,data_value in value.items():
                    self.assert_body(data,data_key,data_value)

